package com.zxl.util.search;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

public class FileUtil {

	public static void main(String[] args) {
		String path = getResourcePath();
		String fileName = path + ConfigPropertiesUtils.getStringValue("keywords_file");
		String content = readFileByChars(fileName);
		System.out.println(content);
		
		String content1 = GetWebContent.getWebCon(ConfigPropertiesUtils.getStringValue("keywords_url"));
		System.out.println(content1);
		
		System.out.println(!content.trim().equals(content1.trim()));
	}
	
	/**
	 * 写文件
	 * @param fileName
	 * @param content
	 * @param rw true为追加，false为覆盖
	 */
	public static void writeFile(String fileName, String content, boolean rw){
		try {
			FileWriter writer = new FileWriter(fileName, rw);
			writer.write(content);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 读取文件
	 * @param fileName
	 * @return
	 */
	public static String readFileByChars(String fileName) {
		
        Reader reader = null;
        StringBuffer sb = new StringBuffer();
        try {
            // 一次读多个字符
            char[] tempchars = new char[30];
            int charread = 0;
            reader = new InputStreamReader(new FileInputStream(fileName));
            // 读入多个字符到字符数组中，charread为一次读取字符数
            while ((charread = reader.read(tempchars)) != -1) {
                // 同样屏蔽掉\r不显示
                if ((charread == tempchars.length)
                        && (tempchars[tempchars.length - 1] != '\r')) {
                    System.out.print(tempchars);
                } else {
                    for (int i = 0; i < charread; i++) {
                        if (tempchars[i] == '\r') {
                            continue;
                        } else {
//                            System.out.print(tempchars[i]);
                            sb.append(tempchars[i]);
                        }
                    }
                }
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        
        return sb.toString();
    }
	
	/**
	 * 获取类的当前目录
	 * @return
	 */
	public static String getResourcePath() {
		String className = FileUtil.class.getName();
		String classNamePath = className.replace(".", "/") + ".class";
		URL is = FileUtil.class.getClassLoader().getResource(classNamePath);
		String path = is.getFile();
		int n = path.lastIndexOf("/");
		path = path.substring(0, n+1);
		
		//如果访问的是jar包里的路径，则返回jar包的当前目录
		int m = path.indexOf("!");
		if(m != -1){
			path = path.substring(0, m);
			m = path.indexOf("/");
			n = path.lastIndexOf("/");
			path = path.substring(m, n+1);
		}

		return path;
	}
}
